<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图像去雾处理</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            text-align: center;
        }
        .container {
            margin-top: 20px;
        }
        .control-panel {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 20px;
            margin-bottom: 20px;
        }
        .buttons {
            display: flex;
            flex-direction: column;
            gap: 10px;
        }
        .params-container {
            padding: 15px;
            background-color: #f5f5f5;
            border-radius: 8px;
            text-align: left;
        }
        .param-group {
            margin: 8px 0;
            display: flex;
            align-items: center;
        }
        .param-group label {
            width: 100px;
            margin-right: 10px;
        }
        .param-group input[type="range"] {
            width: 150px;
        }
        .param-value {
            width: 40px;
            text-align: right;
            margin-left: 10px;
        }
        .images-container {
            display: flex;
            justify-content: center;
            gap: 40px;
            margin-top: 20px;
        }
        .image-box {
            flex: 0 0 500px;
            text-align: center;
        }
        .image-preview-container {
            width: 500px;
            height: 400px;
            border: 1px solid #ddd;
            border-radius: 4px;
            overflow: hidden;
            display: flex;
            align-items: center;
            justify-content: center;
            background-color: #f5f5f5;
            cursor: pointer;
            transition: transform 0.2s;
        }
        .image-preview-container:hover {
            transform: scale(1.01);
        }
        .image-preview {
            max-width: 100%;
            max-height: 100%;
            object-fit: contain;
        }
        .image-title {
            margin: 10px 0;
            font-size: 18px;
            color: #333;
        }
        .button {
            background-color: #4CAF50;
            border: none;
            color: white;
            padding: 12px 24px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            cursor: pointer;
            border-radius: 4px;
            width: 120px;
            transition: background-color 0.3s;
        }
        .button:hover {
            opacity: 0.9;
        }
        .button:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }
        .loading {
            display: none;
            margin: 10px 0;
        }
        .param-info {
            cursor: help;
            margin-left: 5px;
            color: #666;
            font-size: 14px;
        }
        .tooltip {
            position: relative;
            display: inline-block;
        }
        .tooltip .tooltip-text {
            visibility: hidden;
            width: 200px;
            background-color: #555;
            color: #fff;
            text-align: center;
            border-radius: 6px;
            padding: 8px;
            position: absolute;
            z-index: 1;
            left: 50%;
            margin-left: -100px;
            opacity: 0;
            transition: opacity 0.3s;
            font-size: 14px;
            bottom: 125%;
        }
        .tooltip .tooltip-text::after {
            content: "";
            position: absolute;
            top: 100%;
            left: 50%;
            margin-left: -5px;
            border-width: 5px;
            border-style: solid;
            border-color: #555 transparent transparent transparent;
        }
        .tooltip:hover .tooltip-text {
            visibility: visible;
            opacity: 1;
        }
        /* 模态框样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.9);
            cursor: pointer;
        }
        .modal-content {
            margin: auto;
            display: block;
            max-width: 90%;
            max-height: 90vh;
            position: relative;
            top: 50%;
            transform: translateY(-50%);
        }
        .modal-close {
            position: absolute;
            right: 20px;
            top: 20px;
            color: #f1f1f1;
            font-size: 30px;
            font-weight: bold;
            cursor: pointer;
        }
        .modal img {
            width: auto;
            height: auto;
            max-width: 100%;
            max-height: 90vh;
        }
    </style>
</head>
<body>
    <h1>图像去雾处理</h1>
    <div class="container">
        <div class="control-panel">
            <div class="buttons">
                <button type="button" class="button" onclick="document.getElementById('imageInput').click()">选择图片</button>
                <button type="button" class="button" onclick="processImage()" id="processButton" disabled>处理图片</button>
                <button type="button" class="button" onclick="resetParams()" style="background-color: #666;">重置参数</button>
                <input type="file" id="imageInput" accept=".jpg,.jpeg,.png" style="display: none;">
            </div>

            <div class="params-container">
                <div class="param-group">
                    <label for="omega">去雾强度:</label>
                    <input type="range" id="omega" min="0.5" max="1" step="0.05" value="0.75">
                    <span id="omegaValue" class="param-value">0.75</span>
                    <div class="tooltip">
                        <span class="param-info">ⓘ</span>
                        <span class="tooltip-text">控制去雾的程度，值越大去雾效果越强。范围：0.5-1.0</span>
                    </div>
                </div>
                <div class="param-group">
                    <label for="skyThresh">天空阈值:</label>
                    <input type="range" id="skyThresh" min="0.3" max="1.0" step="0.05" value="0.7">
                    <span id="skyThreshValue" class="param-value">0.70</span>
                    <div class="tooltip">
                        <span class="param-info">ⓘ</span>
                        <span class="tooltip-text">控制天空区域的识别范围，值越大识别的天空区域越少。范围：0.3-1.0</span>
                    </div>
                </div>
                <div class="param-group">
                    <label for="skyTrans">天空透射率:</label>
                    <input type="range" id="skyTrans" min="0.5" max="1.5" step="0.05" value="0.85">
                    <span id="skyTransValue" class="param-value">0.85</span>
                    <div class="tooltip">
                        <span class="param-info">ⓘ</span>
                        <span class="tooltip-text">控制天空区域的去雾程度和亮度，值越大天空越亮。范围：0.5-1.5</span>
                    </div>
                </div>
                <div class="param-group">
                    <label for="windowSize">窗口大小:</label>
                    <input type="range" id="windowSize" min="5" max="25" step="2" value="5">
                    <span id="windowSizeValue" class="param-value">5</span>
                    <div class="tooltip">
                        <span class="param-info">ⓘ</span>
                        <span class="tooltip-text">暗通道先验的窗口大小，较大的值会使去雾更均匀，但可能丢失细节。范围：5-25</span>
                    </div>
                </div>
            </div>
        </div>

        <div id="loading" class="loading">处理中...</div>
        <div class="images-container">
            <div class="image-box">
                <h3 class="image-title">原始图片</h3>
                <div class="image-preview-container" onclick="showModal(preview)">
                    <img id="preview" class="image-preview" style="display: none;">
                </div>
            </div>
            <div class="image-box">
                <h3 class="image-title">处理结果</h3>
                <div class="image-preview-container" onclick="showModal(result)">
                    <img id="result" class="image-preview" style="display: none;">
                </div>
            </div>
        </div>
    </div>

    <div id="imageModal" class="modal" onclick="closeModal()">
        <span class="modal-close">&times;</span>
        <div class="modal-content">
            <img id="modalImage">
        </div>
    </div>

    <script>
        const imageInput = document.getElementById('imageInput');
        const preview = document.getElementById('preview');
        const processButton = document.getElementById('processButton');
        const loading = document.getElementById('loading');
        const result = document.getElementById('result');

        // 参数滑块更新显示值
        ['omega', 'skyThresh', 'skyTrans', 'windowSize'].forEach(param => {
            const slider = document.getElementById(param);
            const valueDisplay = document.getElementById(param + 'Value');
            slider.addEventListener('input', () => {
                valueDisplay.textContent = slider.value;
            });
        });

        function processImage() {
            const formData = new FormData();
            formData.append('image', imageInput.files[0]);
            formData.append('omega', document.getElementById('omega').value);
            formData.append('skyThresh', document.getElementById('skyThresh').value);
            formData.append('skyTrans', document.getElementById('skyTrans').value);
            formData.append('windowSize', document.getElementById('windowSize').value);

            loading.style.display = 'block';
            processButton.disabled = true;

            fetch('/process', {
                method: 'POST',
                body: formData
            })
            .then(response => response.blob())
            .then(blob => {
                const url = URL.createObjectURL(blob);
                result.src = url;
                result.style.display = 'block';
                loading.style.display = 'none';
                processButton.disabled = false;
            })
            .catch(error => {
                console.error('Error:', error);
                loading.style.display = 'none';
                processButton.disabled = false;
                alert('处理图片时出错');
            });
        }

        imageInput.addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    preview.src = e.target.result;
                    preview.style.display = 'block';
                    result.style.display = 'none';
                    processButton.disabled = false;
                }
                reader.readAsDataURL(file);
            }
        });

        // 定义默认参数值
        const defaultParams = {
            'omega': 0.75,
            'skyThresh': 0.7,
            'skyTrans': 0.85,
            'windowSize': 5
        };

        // 重置参数函数
        function resetParams() {
            Object.keys(defaultParams).forEach(param => {
                const slider = document.getElementById(param);
                const valueDisplay = document.getElementById(param + 'Value');
                slider.value = defaultParams[param];
                valueDisplay.textContent = defaultParams[param].toFixed(2);
            });
            
            // 如果当前有图片，自动重新处理
            if (!processButton.disabled) {
                processImage();
            }
        }

        const modal = document.getElementById('imageModal');
        const modalImg = document.getElementById('modalImage');

        function showModal(img) {
            if (img.style.display !== 'none') {
                modal.style.display = 'block';
                modalImg.src = img.src;
            }
        }

        function closeModal() {
            modal.style.display = 'none';
        }

        // 按ESC键关闭模态框
        document.addEventListener('keydown', function(e) {
            if (e.key === 'Escape') {
                closeModal();
            }
        });

        // 防止点击图片时关闭模态框
        modalImg.addEventListener('click', function(e) {
            e.stopPropagation();
        });
    </script>
</body>
</html> 